<template>
  <div class="shopcart-container">
    <div class="goods-list">
      <!-- 购物车商品列表 -->
      <div class="mui-card">
        <div class="mui-card-content" v-for="item in goodslist" :key="item.id">
          <div class="mui-card-content-inner flex">
            <!-- 复选框 -->
            <div class="mui-input-row mui-checkbox mui-left">
              <label> &nbsp;</label>
              <input
                type="checkbox"
                v-model="getGoodsSelected[item.id]"
                @change="selectChanged(item.id, getGoodsSelected[item.id])"
                :disabled="item.num === 0"
              />
            </div>
            <!-- 图片 -->
            <img :src="item.image" alt="" />
            <!-- 商品详情 -->
            <div class="info">
              <h1>{{ item.name }}</h1>
              <p class="flex">
                <span class="price">￥ {{ item.price }}</span>
                <numbox
                  :max="item.num"
                  :initcount="getGoodsCount[item.id]"
                  :goodsid="item.id"
                  @count="countChanged"
                  size="min"
                ></numbox>
                <a href="#" @click.prevent="remove(item.id)">删除</a>
              </p>
            </div>
          </div>
        </div>
      </div>
      <!-- 结算区域 -->
      <div class="mui-card">
        <div class="mui-card-content">
          <div class="mui-card-content-inner balance">
            <div class="left">
              <p>总计不含运费</p>
              <p>
                已勾选商品 <span class="red"> {{ getSelectedCount }}</span
                >件, 总价:<span class="red">￥{{ getSelectedAmount }}</span>
              </p>
            </div>
            <mt-button type="primary" @click="createOrder">去结算</mt-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
import numbox from "../components/numbox.vue";
export default {
  name: "shopcart",
  data() {
    return {
      goodslist: [],
    };
  },
  components: {
    numbox,
  },
  computed: {
    ...mapState("shopcart", ["car"]), //获取shopcar模块中car的数据 并将它作为计算属性
    ...mapGetters("shopcart", [
      "getGoodsCount",
      "getGoodsSelected",
      "getSelectedCount",
      "getSelectedGoods",
    ]),
    getSelectedAmount() {
      const selectedGoods = this.getSelectedGoods;
      let amount = 0; //总价
      this.goodslist.forEach((item) => {
        // 判断我们选中的商品 才会去执行if中的代码 进行求总价的操作
        if (selectedGoods[item.id]) {
          // 如果 goodlist中的 有几项的id 在 selectGoods里存在 那么if执行
          amount += item.price * selectedGoods[item.id].count;
        }
      });
      return amount;
    },
  },
  created() {
    this.getGoodList();
  },
  methods: {
    createOrder() {
      if (this.getSelectedCount <= 0) {
        // 没有选中商品 无需下订单
        this.$toast("您没有选择要购买的商品");
        return;
      }
      this.$store.commit("shopcart/setBuy"); //点击去结算的时候 调用无参的setBuy 方法
      this.$router.push("/order/create");
    },
    getGoodList() {
      // 获取购物车中的数据
      // console.log(this.car);
      const idArr = [];
      this.car.forEach((item) => idArr.push(item.id)); //id的集合 便于处理
      this.$http.get("shopcart", { params: { ids: idArr } }).then((res) => {
        // console.log(res.data);
        this.goodslist = res.data.data;
        this.goodslist.forEach((item) => {
          if (item.num === 0) {
            // 将购物车的商品之前的默认选中状态修改为false
            this.selectChanged(item.id, false);
          }
        });
      });
    },
    // 每一次点击+-都会触发 coutChange 通过封装组件的emit方法 将含有id 和count的对象传来
    // 最后同步更新 mutation中的数据 和
    countChanged(goodsinfo) {
      // console.log(goodsinfo); //在numbox组件中封装触发自定义函数 所携带的参数对象
      this.$store.commit("shopcart/updateGoodsInfo", goodsinfo);
    },
    selectChanged(id, selected) {
      this.$store.commit("shopcart/updateGoodsSelected", {
        id: id,
        selected: selected,
      });
    },
    remove(id) {
      // 因为添加的时候 car存在于数组 vuex 和 localstroge
      // 删除goodlist中的商品
      this.goodslist.some((item, index) => {
        if (parseInt(item.id) === id) {
          this.goodslist.splice(index, 1);
          return true;
        }
      });
      // 删除 vuex 和localstroge
      this.$store.commit("shopcart/removeShopcart", id);
    },
  },
};
</script>
<style lang="scss" scoped>
.flex {
  display: flex;
}
.shopcart-container {
  background: #eee;
  overflow: hidden;
  .goods-list {
    .mui-card-content-inner {
      align-items: center;
      padding: 10px;
      .mui-checkbox.mui-left input[type="checkbox"] {
        left: 0px;
      }
      .mui-radio.mui-left label,
      .mui-checkbox.mui-left label {
        padding-left: 20px;
        padding-right: 35px;
        padding-bottom: 22px;
      }
    }
    img {
      width: 60px;
    }
    .info {
      margin-left: 10px;
      width: 100%;
      overflow: hidden;
      box-sizing: border-box;
      h1 {
        text-align: left;
        font-size: 13px;
        font-weight: bold;
        line-height: 20px;
        padding-top: 10px;
      }
      p {
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        .price {
          text-align: left;
          font-size: 16px;
          font-weight: 700;
          color: red;
          flex: 1;
        }
        a {
          line-height: 20px;
        }
      }
    }
  }
  .balance {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .red {
      color: red;
      font-weight: bold;
      font-size: 16px;
    }
  }
}
</style>